2. structures
在这个视频中,我们将讨论我输入的另一种不同类型的进展。
And this video, we will talk about another different type of advance that I typed.
后面我们会用到原因和这种类型的数据就是结构体。
We will use later on the cause and this type of data is the structure.
我们看到的数组是数据的集合,是相同类型的变量类型。
We are seeing arrays that are collections of data, type of variables of the same type.
结构在某种程度上非常相似,因为它们是变量的集合,这些变量可以在单个名称下具有不同类型。
Structures are quite similar in a certain way because they are a collection of variables that can be of different types under a single name.
举例来说,假设我们希望在程序中包含一些变量,例如对某种类型的电子元件进行分类。
So, for instance, let's say that we had that we wanted to have some variables in our program that are, let's say, classifying some type of electronical components.
假设我们正在对一些电动机进行分类。
Let's say that we are classifying some electrical motor.
因此,我们实际上需要有尽可能多的与此类电机相关的不同变量。
So we would need to actually have as many different variables that are related to that kind of motor.
假设我们需要知道电机的额定电流。
So let's say that we had that we needed to know, let's say the motor nominal current.
这可能是一个实型数据类型的变量,表示我们需要知道电机最大电流。
And this may be a real type of data type of variable that said that we needed to know the motor maximal current.
我们正在谈论电动机。
We are talking about electric motors.
因此,我们还需要知道例如电机是否为标称速度,以及例如双整数变量 sales、无符号整数变量。
So we would also need to know if, for instance, the motor, let's say, nominal velocity, and that this would be, for instance, and double integer variable sales, unsigned, integer variable.
我不知道,也许我们需要知道,呃,呃,假设电机类型,呃,假设这是一个无符号整数变量。
And I don't know, maybe we would need to know if the, uh, well, uh, let's say the motor type, uh, let's say that this is an unsigned integer variable.
我们需要说,让我们添加一个代码。
And we will need to say, let's add a code to this.
所以对我们来说,零意味着该电机是异步电机。
So for us, zero will mean that this motor is an asynchronous motor.
一则意味着有无刷电机。
One would mean that there's are brushless motor.
比方说,对我来说,电机是,呃,步进电机,三是这个电机。
Let's say that to me is that the motor is, uh, stepper motor and the three is this motor.
假设这是我们对该变量的方式或代码。
Let's say that this is our way or our code for this variable.
所以我们有不同的信息,这些信息都与电机相关。
So we have different informations that are all related to the motor.
所以我们在这里可以做的是,实际上,嗯,我们在这里可以做的是,如果我们有很多电机,正如我们在上一个视频中看到的那样,我们可以简单地拥有,呃,数组。
So what we could do over here is to actually, um, what we could do over here is to if we have many motors, as we saw in the previous video, we could simply have, uh, arrays.
所以你有不同的变量,不同的电流集,呃,标称速度等等,并且有许多不同的数组单元来实际引用不同类型的电机。
So you have different variables, different set of currents, set of, uh, nominal velocities and so on, and have many different cells of an array to actually refer to the different types of motors.
但另一种方法可能是定义一个我们称之为电机数据的对象,并在电机数据下获得特定电机的所有不同信息。
But another way could be to define an object that we call, for instance, motor data and under motor data to have all this different informations for the specific motor.
我们如何才能做到这一点,我们需要在这里进行应用。
And how can we do that, which we need to go here into application.
正确的。
Right.
单击,添加对象。
Click, add object.
我们需要做的是添加 ADT。
And what we need to do is add the ADT.
那是一种数据单元类型。
That is a data unit type.
正如您所看到的,我们有不同类型的类型。
As you can see, we have different types of that in type.
我们现在正在寻找电机、结构和类型。
We are looking right now to the motor, to the structure, that type.
所以我们选择结构。
And so we select the structure.
例如,在这里我们可以编写电机数据,并执行此操作,我们正在定义一个新的自定义类型,该类型将出现在我们的策略配置中。
And over here we may write, for instance, that the motor data and doing this, we are defining a new custom type that will appear over here on our policy configuration.
该类型已被跟踪,如果您打开它,您将在此处看到。
The type is tracked and if you open it, you will see over here.
这些符号被跟踪和拉伸,在这里我们可以定义电机数据中的不同变量。
This notations tracked and stretched, and over here we can define the different variables that are within the motor data.
因此,正如我们之前所说,我们可以在电机数据中写入,我们需要标称电流,正如您所说,它是一个实际值,最大电流,我想我说的是分配双整数的标称速度。
So we can write, as we said before, in what within our motor data, we would need the nominal current that has, as you said, a real value, the max current, I think I said the nominal velocity that was assigned double integer.
让我们看看变量是什么,类型是什么,正如我所说,类型不能被使用,因为它对于语言来说是一个重要的词。
And let's see what are what are the variables, the type that, as I said, was, let's say type cannot be used because it is an important word for the language.
假设这是电机类型。
So let's say that this is motor type.
正如我所说,我猜这是一个整数。
And as I said, this was an integer, I guess.
嘿,我们添加了这个,呃,这个符号,表明我们正在写这篇评论。
And hey, we added this, uh, this notation for us that we were writing this comment.
现在我们已经定义了电机数据,在这里我们实际上可以删除它并简单地分配该类型的电机的一个变量。
So right now we have defined the motor data and over here we could actually remove this and simply allocate the one single variable of motor that the type.
那么我可以说是我们使用该电机的时间吗?
So could I say is the let's say the time that we are using this motor for a.
所以我要告诉你的电机,这是电机数据的类型。
So I will say convey your motor that this was type is motor data.
如果我在模拟模式下构建并上线,我在这里分配了这个变量。
And here I have allocated this variable if I build and go online in simulation mode.
实际上,您在这里只会看到一个变量,在该变量下方,我们有我们定义的四个变量。
You will actually see over here only one variable and beneath the variable, we have the four variables that we have defined.
因此,如果我想通过代码访问这些变量,如果我将其放置在确认完成代码功能之后,我实际上可以编写、传送或传递这些变量,该代码功能实际上会建议我可以使用的变量。
So if I were if I wanted to access those variables by code, I could actually write, convey or motor in if I place that after a confirmatory was set out to complete a feature of code that will actually suggest the variables that I can use.
正如您所看到的,变量的注释一旦越过它们就会显示出来。
As you can see, also, the comments of the variable will be displayed as soon as they pass over over them.
然后我就可以简单地访问所有这些数据。
And then I could simply access all this data.
例如,我可以在代码中说,如果我上网下载,模型,呃,最大电流等于两安培。
For instance, I could say in the code that the model, uh, maximal current is equal to two amper if I go online with a download.
正如你现在所看到的,当孩子跑步时,我们正在写信到这里。
As you can see right now, when the kid is running, we are writing to over here.
所以这是当代码直接运行时,如果你想初始化结构体的值,它实际上比初始化变量要棘手一点,你应该做下面的事情,你应该玩一些等于你需要打开的东西括号周围。
So this is when the code is running directly, if you want to initialize the value of the structure, it is actually a little bit trickier than rather than initializing variable and you shall do what follows, you should play some equal to you need to open around brackets.
然后在这个括号中,您将需要像平常一样实际初始化变量的所有值。
And then over here in this brackets, you will need to actually initialize all the values of the variables as we as we normally do.
所以我会在这里写,例如,标称电流,应该等于两点零,呃,最大电流等于四点零,比方说标称速度,等于三千转每分钟。
So I would write over here, nominal current, for instance, that is should be equal to two point zero, uh, max current equal to four point zero, let's say nominal velocity, equal to three thousand r.p.m.
最后一个是母型。
and the last one shall be the mother type.
可以说这是一个广泛的刷子或更聪明的刷子。
Let's say that this is a broad brush or smarter.
我说这是给我们的。
And I said this was one for us.
所以这样来说,我说要转换你的电机就是电机数据类型和初始值或者结构体的字段。
So in this way, I'm saying that to convert your motor is the motor data type and the initial values or the fields of the structure.
这是。
And this is.
如果我执行下载,我会开始,如您所见,将这些值写入结构中,结构对于对属于同一对象、单个目的、单个目的的数据进行分组非常有用。单一功能,在开发函数和功能块接口时非常有用。
And if I carry out the download, I start, as you can see, with those values written in the structure, structures are really useful to group data that belong to a same, uh, to a single object, to a single purpose, to a single functionalities, and are quite useful when you are developing a functions and function block interfaces.
例如,正如我们在上一个视频中所做的那样,我们也可以说我们的程序中的机器中有 20 个监视器。
So, for instance, as we did in the previous video, we can also let's say that we have 20 monitors in our, uh, in our machine in our program.
我可以简单地在传送带电机上输入一个范围从 1 到 20 个电机数据的数组。
I could simply over right at the conveyor motor equal to an array that ranges from one to 20 of motor data.
这样一来,如果我上网的话,抱歉,我写错了。
And in this way, if I go online, sorry, I wrote something wrong.
所以问题就在这里。
So what's wrong is over here in a second.
所以如果我赢了,我就需要改变这个名字。
So if I win, I would need to change this name.
假设这些是机器电机。
So let's say that these are the machine motors.
所以如果我上网的话,对不起,这不是一种欺凌类型,我改变了,如果我现在上网的话,它是一种元数据。
So if I go online, sorry, and it's not a bullying type, I changed, it was a metadata data if I go online right now.
我会看到这是一个数组,在它下面有 20 种电机数据类型,在每一种数据类型中都有我们定义的四个变量。
I will see this is an array, and beneath this we have 20 motor data types and within each one of them we have those four variables that we have defined.
因此,如果您想初始化数组的某些初始值,则需要使用不同的表示法。
So if you want to initialize to certain initial values of array's, you will need to have a different notation.
因此初始化结构数组可能非常棘手。
So initializing arrays of structures can be quite tricky.
请记住,您实际上可以用您的定义来标记。
Remember that you can actually you'll be labeled with your definition.
您实际上可以添加一个段落。
You can actually add a paragraph.
而这实际上是允许的。
And this is actually allowed.
因此,如果要定义初始化数组的值,则需要将所有内容放在方括号之间,并且数组的每个值都需要用逗号分隔。
So if you want to define the initialize the values of the array, you need to put everything between square brackets and each value of the array needs to be divided to the other with a comma.
由于我们正在定义结构,因此我们需要像以前一样使用圆括号来初始化它们。
And since we are defining the structures, we need to initialize them as before with the round brackets.
因此,举例来说,假设我们想要第一个电流需要标称电流,比方说,像以前一样等于两个零点。
So, for instance, let's say that we want to the first one will need a nominal current, let's say, equal to two point zero as before.
多路复用器电流等于 4.0。
Mux current equals 4.0.
电机类型抱歉,最大标称速度应等于三千。
The motor type sorry, the max nominal velocity was velocity shall be equal to three thousand.
假设最后一个模型类型等于无刷。
And let's say the last one was the model type is equal to brushless.
那等于一。
That was equal to one.
然后第二个说标称电流等于每1,最大电流等于每2点0,标称速度为1500,原型类型仍然是无刷的。
Then the second one there say that a nominal current is equal to one per the maximum current is equal to two point zero per, the nominal velocity is 1500 and the prototype type is still brushless.
让我们这样做,然后您可以在启动所有 20 个电机时继续初始化它们。
Let's do it this way and then you can go on and initialize them as you go all 20 motors.
但这实际上有时非常棘手,因为您实际上需要初始化数组的所有值,即使您只想初始化一个单元格。
But this is actually sometimes quite tricky because you actually need to initialize all the values of the array, even if you want to initialize just a single cell.
现在,如果我上网,我们看看是否有错误。
So right now, if I go online, let's see if there are no errors.
如果我研究一下,我会发现,正如您所看到的,第一个是初始化的值,第二个和其他都是空的,带有变量的默认值。
And if I look into this, I will find, as you can see, the first one is with the values that are initialized, the second one and the others are empty with the default values of the variables.
正如您所看到的,初始化这样的所有内容可能非常棘手,因为您实际上可能会丢失正在初始化的索引。
So as you can see, it may be quite tricky to initialize everything like this because you may actually lose track of the index that you are initializing.
所以有时我会说,好吧,我只是在这里添加注释,这实际上可以帮助您了解您正在初始化的索引是什么。
So sometimes I say, well, I'm just here to add the comment over here that will actually help you to know what is the index that you're initializing.
我实际上建议为其正在初始化的结构启动一个新行。
I actually suggest to start a new line for its structure that are initializing.
实际上常用的另一种可能性是避免在变量定义中进行初始化。
And another possibility that is actually quite used is to avoid an initialization in the in the definition of the variables.
也许你实际上可以在代码中做到这一点。
And maybe you can actually do this in the code.
因此,您实际上可以做的是创建一个仅在中华人民共和国开始时运行一次的操作。
So what you can do is actually create an action that will run just one time at the start of the of the PRC.
因此,我可以单击程序,添加一个操作,将其解释为操作的结构化文本,我们将其称为初始化电机数据。
So I can click on the program, add an action, explain this as a structured text to action and let's call this initialize motor data.
在这里,我们有它。
Here we have it.
在此操作中,我们可以实际将值分配给结构的变量。
And in this action, you can we can actually assign the values to the variables of the structure.
所以我称这台机器为电机。
So what I call this machine motors.
我说第一个应该有最大的车,然后让我们重新开始。
I say the first one should have the max car and let's say start again.
机器电机的标称电流再次等于 2.0。
The nominal current equals 2.0 again, machine motors.
第一个,Max Karanth,应该等于四点零机器电机。
The first one, the Max Karanth, should be equal to four point zero machine motors.
一应等于 或 电机。
One should be equal to or the motor.
抱歉,标称速度等于三千。
Sorry, the nominal velocity was equal to three thousand.
最后一项是电机类型等于一。
And the last one was the motor type was equal to one.
这里还有一条评论说这是电机一号,然后我们可以简单地复制并粘贴此代码。
And also over here had a comment saying that this is motor one and then we can simply copy and paste this code.
您可以更改所有数组索引。
You can change all the array indexes.
但我一一建议了这个简单的调整。
But one by one I suggested this this simple tweak.
因此,如果按住alt按钮,则可以进行多行选择,然后取消索引并写入另一行。
So if you hold the alt button, you can carry out a multiline selection and then cancel the index and write another one.
当您执行这种,嗯,这种,呃,这种调整这些程序时,这非常有用。
This is quite useful when you are doing this kind of, um, this, uh, this kind of tweaks these kind of procedures.
我在这里可以更改值等等。
And I here I can change the values and so on.
对所有电机执行此操作。
Do this for all the motors.
因此,为了让此操作仅运行一次,您可以添加布尔变量。
So in order to have this action running only once you can add the boolean variables.
假设我们称其为“更多的电机初始化”,而不是“不可行”。
Let's say let's call this more motor initialization than it is about inviable.
在这里,程序的开头,我们有一条评论说这是电机初始化。
And over here, the beginning of the program, we had a comment saying that this is the motor initialization.
并说如果我们没有这个变量等于 true。
And say that if we do not have this that variable equal to true.
因此,如果没有更多的初始化,那么我们应该到目前为止调用该操作,对吗?
So if not more to initialization, then then we should call that action so far, is that right?
初始化版主价格控制空间,你会看到自动感觉开闭括号和分号处,然后电机初始化则应等于true。
Initialize moderator price control space, you will see automatically feels open and close brackets and at the semicolon, then motor initialization then shall be equal to true.
通过这种方式,您可以确定,如果您从未在代码的其他部分触及这些变量,您将只输入一次 if 语句,因为在第二个 p.l.c.
In this way you are sure if you never touch these variables in other part of the code, you will enter into this if statement only once, because at the second p.l.c.
循环我们将拥有等于真实的机动化,并且该代码将不再运行。
cycle we will have this motorization that equals the true and this code will run no more.
因此,如果我上网并进行下载,正如您在运行程序之前所看到的那样,所有结构的值都为零。
So if I go online and carry out a download, as you can see before I run the program, all the structures have zero in their values.
然后,如果开始,如您所见,值已被修改。
And then if a start, as you can see, the values have been modified.
事实上,他们并没有在每个政策周期中都写下这一点,因为这项行动没有实施。
And actually, they are not being written that every policy cycle because this action is not running.
因此,例如,我可以在在线时更改这些值。
So, for instance, I could change the values when I'm online.
所以结构非常有用。
So structures can be quite useful.
我们还看到了具有结构数组的变体。
We have also seen the variant with arrays of structures.
在下一个视频中,我们将了解另一种信息丰富、非常有用的类型,我们将在课程中大量使用它们。
In the next video we will look at another type that is informative, that are quite useful, and we will use them quite a bit in the course.